﻿namespace dogd
{
	using System.Data.Common;
	using System.Data.SQLite;
	using System;

	/// <summary>
	/// Implementation of IParameterFactory targetted at creating SQLiteParameter instances.
	/// </summary>
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Sqlite", Justification = "It is spelled correctly!")]
	public class SqliteParameterFactory : IParameterFactory
	{
		/// <summary>
		/// Create a parameter object (for select) based on a key value and column metadata
		/// </summary>
		/// <param name="key"></param>
		/// <param name="metadata"></param>
		/// <returns></returns>
		public DbParameter CreateReadParameter(Key key, ColumnMetadata metadata)
		{
			var parameter = new SQLiteParameter(metadata.Parameter, metadata.StorageType);
			parameter.Value = key.Value;

			return parameter;
		}

		/// <summary>
		/// Create a parameter object (for select) based on a string id and column metadata
		/// </summary>
		/// <param name="id"></param>
		/// <param name="metadata"></param>
		/// <returns></returns>
		public DbParameter CreateReadParameter(string id, ColumnMetadata metadata)
		{
			var parameter = new SQLiteParameter(metadata.Parameter, metadata.StorageType);
			parameter.Value = id;

			return parameter;
		}

		/// <summary>
		/// Create a write parameter (insert or update) based on a value and column metadata
		/// </summary>
		/// <param name="value"></param>
		/// <param name="metadata"></param>
		/// <returns></returns>
		public DbParameter CreateWriteParameter(object value, ColumnMetadata metadata)
		{
			var parameter = new SQLiteParameter(metadata.Parameter, metadata.StorageType);
			parameter.Value = Convert.ChangeType(value, metadata.PropertyType);

			return parameter;
		}
	}
}
